<?php



/**

 * @author ducnhat

 * @email ddnhat@gmail.com

 * @copyright 2014

 */



class Default_Model_Order extends Zend_Db_Table{

	

	protected $_name    = "orders";

    protected $_primary = "id";

    

    public function saveData($data = array()){

    	$db = Zend_Registry::get('db');

        

        $db->beginTransaction();

        

        try{

            if(isset($data['id']) && $this->checkExists($data['id'])){

                $id = $data['id'];

                $db->update("orders", $data, "id = $id");

            }else{

                $db->insert('orders', $data);

                

                $id = $db->lastInsertId("orders");

            }

            

            $cache = new Zenddn_System_Cache();

            $cache->clean(array('order'));

            

            $db->commit();

            

            return $id;

        }catch(Exception $e){

            $db->rollBack();

            

            return false;

        }

    }

    

    public function getData($id = null, $email = null, $status = null){

    	$db = Zend_Registry::get('db');

        $db->setFetchMode(Zend_Db::FETCH_OBJ);

        

        $select = $db->select()

                    ->from(array('o' => 'orders'), array('id', 'name', 'email', 'address', 'phone_number', 'date', 'purchase_date', 'total', 'items', 'status'))

                    ->order('o.id DESC');

        

        if($status){

            $select->where("o.status = 1");

        }else if(!$status){

            $select->where("o.status = 0");

        }

        

        if($id != null){

            $select->where("o.id = ?", $id, 'INTEGER');

            

            $db->setFetchMode(Zend_Db::FETCH_ASSOC);

            $result = $db->fetchRow($select);

        }else if($email != null){

            $select->where("o.email = ?", $email);

            

            $result = $db->fetchAll($select);

        }else{

            $result = $db->fetchAll($select);

        }

        

        return $result;

    }

    

    public function getAllData($status = null){

    	$cache = new Zenddn_System_Cache();

        $cache = $cache->getCache();

        $cache_id = "order_getAllData_{$status}";

        

        if(($result = $cache->load($cache_id)) === false){

            $result = $this->getData(null, null, $status);

            

            $cache->save($result, $cache_id, array('order'));

        }

        

    	return $result;

    }

    

    public function getDataByEmail($email, $status = null){

    	$cache = new Zenddn_System_Cache();

        $cache = $cache->getCache();
        $email_replace = str_ireplace("@", "", $email);
        $email_replace = str_ireplace(".", "", $email_replace);
        $cache_id = "order_getDataByEmail_{$email_replace}_{$status}";

        

        if(($result = $cache->load($cache_id)) === false){

            $result = $this->getData(null, $email, $status);

            

            $cache->save($result, $cache_id, array('order'));

        }

        

    	return $result;

    }

    

    public function deleteData($id){

    	$db = Zend_Registry::get('db');

        

        $db->beginTransaction();

        

        try{

            $db->delete('orders', "id = $id");

            

            $cache = new Zenddn_System_Cache();

            $cache->clean(array('order'));

            

            $db->commit();

            

            return true;

        }catch(Exception $e){

            $db->rollBack();

            

            return false;

        }

    }

    

    public function checkExists($id){

        $option = array('table' => 'orders', 'field' => 'id');

    	$validator = new Zend_Validate_Db_RecordExists($option);

        

        if($validator->isValid($id)){

            return true;

        }

        

        return false;

    }

}